package com.krmes.mes.util;

import com.loopj.android.http.BuildConfig;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HTMLUtil {
    private static final String regEx_html = "<[^>]+>";
    private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>";
    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>";
    private static final String regxpForHtml = "<([^>]*)>";

    public static String delHTMLTag(String htmlStr) {
        return Pattern.compile(regEx_html, 2).matcher(Pattern.compile(regEx_style, 2).matcher(Pattern.compile(regEx_script, 2).matcher(htmlStr).replaceAll(BuildConfig.VERSION_NAME)).replaceAll(BuildConfig.VERSION_NAME)).replaceAll(BuildConfig.VERSION_NAME).trim();
    }

    public static String replaceTag(String input) {
        if (!hasSpecialChars(input)) {
            return input;
        }
        StringBuffer filtered = new StringBuffer(input.length());
        for (int i = 0; i <= input.length() - 1; i++) {
            char c = input.charAt(i);
            switch (c) {
                case '\"':
                    filtered.append("&quot;");
                    break;
                case '&':
                    filtered.append("&amp;");
                    break;
                case '<':
                    filtered.append("&lt;");
                    break;
                case '>':
                    filtered.append("&gt;");
                    break;
                default:
                    filtered.append(c);
                    break;
            }
        }
        return filtered.toString();
    }

    public static boolean hasSpecialChars(String input) {
        boolean flag = false;
        if (input != null && input.length() > 0) {
            for (int i = 0; i <= input.length() - 1; i++) {
                switch (input.charAt(i)) {
                    case '\"':
                        flag = true;
                        break;
                    case '&':
                        flag = true;
                        break;
                    case '<':
                        flag = true;
                        break;
                    case '>':
                        flag = true;
                        break;
                    default:
                        break;
                }
            }
        }
        return flag;
    }

    public static String filterHtml(String str) {
        Matcher matcher = Pattern.compile(regxpForHtml).matcher(str);
        StringBuffer sb = new StringBuffer();
        for (boolean result1 = matcher.find(); result1; result1 = matcher.find()) {
            matcher.appendReplacement(sb, BuildConfig.VERSION_NAME);
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String fiterHtmlTag(String str, String tag) {
        Matcher matcher = Pattern.compile("<\\s*" + tag + "\\s+([^>]*)\\s*>").matcher(str);
        StringBuffer sb = new StringBuffer();
        for (boolean result1 = matcher.find(); result1; result1 = matcher.find()) {
            matcher.appendReplacement(sb, BuildConfig.VERSION_NAME);
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String replaceHtmlTag(String str, String beforeTag, String tagAttrib, String startTag, String endTag) {
        String regxpForTag = "<\\s*" + beforeTag + "\\s+([^>]*)\\s*>";
        String regxpForTagAttrib = new StringBuilder(String.valueOf(tagAttrib)).append("=\"([^\"]+)\"").toString();
        Pattern patternForTag = Pattern.compile(regxpForTag);
        Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
        Matcher matcherForTag = patternForTag.matcher(str);
        StringBuffer sb = new StringBuffer();
        for (boolean result = matcherForTag.find(); result; result = matcherForTag.find()) {
            StringBuffer sbreplace = new StringBuffer();
            Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1));
            if (matcherForAttrib.find()) {
                matcherForAttrib.appendReplacement(sbreplace, new StringBuilder(String.valueOf(startTag)).append(matcherForAttrib.group(1)).append(endTag).toString());
            }
            matcherForTag.appendReplacement(sb, sbreplace.toString());
        }
        matcherForTag.appendTail(sb);
        return sb.toString();
    }
}
